﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Store
{
    public class RoleAuthorityDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHStore);
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByUser(int uid, int mid, string tag)
        {
            var sql = "SELECT COUNT(1) FROM Permission.RoleAuthority t1 INNER JOIN Permission.UserRoleAssign t2 ON t1.RoleID = t2.RoleID WHERE [ModuleID] = @RA_MID AND [AuthorityTag] = @RA_ATag AND t2.UserID = @uid";
            SqlParameter[] parameters = {
					new SqlParameter("@uid",uid),
                    new SqlParameter("@RA_MID",mid),
                    new SqlParameter("@RA_ATag",tag)
			};
            return DbHelperSQL.Exists(sql, parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int roleid, int mid, string tag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Permission.RoleAuthority");
            strSql.Append(" where [RoleID]=@RA_RID and [ModuleID]=@RA_MID and [AuthorityTag]=@RA_ATag");
            SqlParameter[] parameters = {
					new SqlParameter("@RA_RID",roleid),
                    new SqlParameter("@RA_MID",mid),
                    new SqlParameter("@RA_ATag",tag)
			};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        public bool Delete(int Roleid, string mtid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission.RoleAuthority ");
            strSql.Append(" where RoleID=@RA_RID and [ModuleID] IN (SELECT [ModuleID] FROM [Permission].[Module] WHERE ModuleTypeID=@mtid)");
            SqlParameter[] parameters = {
					new SqlParameter("@RA_RID",Roleid),
                    new SqlParameter("@mtid", mtid)
			};

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RoleAuthorityID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[RoleAuthority]");
            strSql.Append(" where RoleAuthorityID=@RoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleAuthorityID", SqlDbType.Int,4)
			};
            parameters[0].Value = RoleAuthorityID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RoleAuthorityEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[RoleAuthority](");
            strSql.Append("RoleID,ModuleID,AuthorityTag)");
            strSql.Append(" values (");
            strSql.Append("@RoleID,@ModuleID,@AuthorityTag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@AuthorityTag", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.ModuleID;
            parameters[2].Value = model.AuthorityTag;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RoleAuthorityEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[RoleAuthority] set ");
            strSql.Append("RoleID=@RoleID,");
            strSql.Append("ModuleID=@ModuleID,");
            strSql.Append("AuthorityTag=@AuthorityTag");
            strSql.Append(" where RoleAuthorityID=@RoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@AuthorityTag", SqlDbType.NVarChar,50),
					new SqlParameter("@RoleAuthorityID", SqlDbType.Int,4)};
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.ModuleID;
            parameters[2].Value = model.AuthorityTag;
            parameters[3].Value = model.RoleAuthorityID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RoleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[RoleAuthority] ");
            strSql.Append(" where RoleID=@RoleID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4)
			};
            parameters[0].Value = RoleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除某菜单项的所有授权数据
        /// </summary>
        public bool DeleteByModuleId(int ModuleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[RoleAuthority] ");
            strSql.Append(" where ModuleID=@ModuleID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleID", SqlDbType.Int,4)
			};
            parameters[0].Value = ModuleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string RoleAuthorityIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[RoleAuthority] ");
            strSql.Append(" where RoleAuthorityID in (" + RoleAuthorityIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RoleAuthorityEntity GetModel(int RoleAuthorityID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RoleAuthorityID,RoleID,ModuleID,AuthorityTag from [Permission].[RoleAuthority] ");
            strSql.Append(" where RoleAuthorityID=@RoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleAuthorityID", SqlDbType.Int,4)
			};
            parameters[0].Value = RoleAuthorityID;

            RoleAuthorityEntity model = new RoleAuthorityEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RoleAuthorityEntity DataRowToModel(DataRow row)
        {
            RoleAuthorityEntity model = new RoleAuthorityEntity();
            if (row != null)
            {
                if (row["RoleAuthorityID"] != null && row["RoleAuthorityID"].ToString() != "")
                {
                    model.RoleAuthorityID = int.Parse(row["RoleAuthorityID"].ToString());
                }
                if (row["RoleID"] != null && row["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(row["RoleID"].ToString());
                }
                if (row["ModuleID"] != null && row["ModuleID"].ToString() != "")
                {
                    model.ModuleID = int.Parse(row["ModuleID"].ToString());
                }
                if (row["AuthorityTag"] != null)
                {
                    model.AuthorityTag = row["AuthorityTag"].ToString();
                }
            }
            return model;
        }
    }
}
